package org.aurora.framework.boot.mybatis.flex.processor.config;

import org.aurora.framework.boot.mybatis.flex.processor.utils.FileUtil;
import org.aurora.framework.boot.mybatis.flex.processor.utils.YamlUtil;

import javax.annotation.processing.Filer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

/**
 * name: GenerateConfig
 * <p>
 * desc: 生成配置
 * </p>
 * create date: 2024-10-09 17:52:57
 *
 * @author cao.yong
 */
public class GenerateConfig {

    public static final String CONFIG_FILE_NAME = "generator-config";
    public static final String[] CONFIG_FILE_EXTENSIONS = new String[] { "yaml", "yml" };

    private boolean enabled = true;

    private String author;

    private String namingStrategy = "UpperCase";

    private EntityConfig entity = new EntityConfig();

    private MapperConfig mapper = new MapperConfig();

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getNamingStrategy() {
        return namingStrategy;
    }

    public void setNamingStrategy(String namingStrategy) {
        this.namingStrategy = namingStrategy;
    }

    public EntityConfig getEntity() {
        return entity;
    }

    public void setEntity(EntityConfig entity) {
        this.entity = entity;
    }

    public MapperConfig getMapper() {
        return mapper;
    }

    public void setMapper(MapperConfig mapper) {
        this.mapper = mapper;
    }

    @Override
    public String toString() {
        return "GenerateConfig{" +
                "enabled=" + enabled +
                ", author='" + author + '\'' +
                ", namingStrategy='" + namingStrategy + '\'' +
                '}';
    }

    public static GenerateConfig load(Filer filer) {
        GenerateConfig config = null;
        for (String extension : CONFIG_FILE_EXTENSIONS) {
            String filename = CONFIG_FILE_NAME + "." + extension;
            try (InputStream stream = FileUtil.getInputStream(filer, filename)){
                if (stream == null) {
                    continue;
                }
                Optional<GenerateConfig> load = YamlUtil.load(stream, GenerateConfig.class);
                if (load.isPresent()) {
                    config = load.get();
                    break;
                }
            } catch (IOException ignore) {
            }
        }
        return config;
    }
}
